<?php


namespace app\community\controller;


use app\common\controller\Common;
use think\Controller;
use think\Db;
use think\Exception;
use think\Session;

class Content extends Controller
{

    public function _initialize()
    {
        //获取平台标题
        $platform = Db::name("config_platform")->find();
        $this->assign("platform", $platform);


        $swidthStyle = Session::get("style");
        $this->assign("swidthStyle",$swidthStyle);
    }

    //获取内容列表
    public function index()
    {
        $colonyId = empty($_GET['colony_id']) ? 0 : $_GET['colony_id'];
        $classify_id = empty($_GET['classify_id']) ? 0 : $_GET['classify_id'];
        $this->assign("colony_id", $colonyId);
        $this->assign("classify_id", $classify_id);
        $keyword = $status = '';
        //获取内容列表
        if(!empty($_GET['keyword'])){
            $keyword = $_GET['keyword'];
            $where['a.title'] = ['like',"%{$keyword}%"];
        }
        if(!empty($_GET['status'])){
            $status = $_GET['status'];
            $where['a.status'] = $status;
        }
        $this->assign("keyword", $keyword);
        $this->assign("status", $status);
        if(!empty($classify_id)){
            $where['a.classify_id'] = $classify_id;
        }else{
            $where['c.colony_id'] = $colonyId;
        }

        if(!empty($_GET['type'])){
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $list = Db::name("admin_content")->alias("a")
                    ->join("community_article_classify b","a.classify_id = b.id")
                    ->join("community_nav c","b.nav_id = c.id")
                    ->field("a.*,b.name propertyName,c.name c_name,b.type")
                    ->limit($start,$limit)
                    ->where($where)->order("a.id desc")->select();
                $count = Db::name("admin_content")->alias("a")
                    ->join("community_article_classify b","a.classify_id = b.id")
                    ->join("community_nav c","b.nav_id = c.id")
                    ->where($where)->count();
                foreach ($list as $k=>$v){
                    $list[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
                }
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            }catch (Exception $e){
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
            }
            return $black;
        }

        return $this->fetch('index');
    }

    //志愿者队伍
    public function teamList(){
        $keyword = '';
        $id = empty($_GET['id'])?0:$_GET['id'];
        $this->assign("id", $id);
        $this->assign("keyword", $keyword);
        $sex = Db::name("config_attribute")->where(['key' => "sex"])->value("value");
        $sex = explode("|", $sex);
        foreach ($sex as $k => $v) {
            $sex[$k + 1] = $v;
        }
        unset($sex[0]);

        if (!empty($_GET['keyword'])) {
            $query['keyword'] = $keyword = $_GET['keyword'];
            $where['b.name'] = ['like', "%{$keyword}%"];
        }
        //获取栏目ID
        $classify_id = Db::name("admin_content")->where(['id'=>$id])->value("classify_id");
        $this->assign("classify_id",$classify_id);
        //获取景区id
        $colony_id = Db::name("admin_content")->alias("a")->join("community_article_classify b","a.classify_id = b.id")->where(['a.id'=>$id])->value("b.colony_id");
        //查询加入的志愿者
        $query["id"] = $id;
        $where['b.colony_id'] = $colony_id;
        $where['a.content_id'] = $id;
        $data = Db::name("community_content_user")->alias("a")
            ->join("system_registration b", "a.fans_id = b.id")
            ->where($where)->field("a.*,b.name,b.sex,b.mobile")->paginate(10, false, ['query' => $query]);
        $list = $data->items();
        foreach ($list as $k => $v) {
            $list[$k]['sex'] = $sex[$v['sex']];
        }
        $assign = [
            'keyword' => $keyword,
            'data' => $data,
            'list' => $list
        ];
        $this->assign($assign);
        return $this->fetch('teamList');
    }

    //报名名单
    public function registration(){
        $keyword = '';
        $id = empty($_GET['id'])?0:$_GET['id'];
        $this->assign("id", $id);
        $this->assign("keyword", $keyword);
        $sex = Db::name("config_attribute")->where(['key' => "sex"])->value("value");
        $sex = explode("|", $sex);
        foreach ($sex as $k => $v) {
            $sex[$k + 1] = $v;
        }
        unset($sex[0]);
        //报名状态
        $registration_status = Db::name("config_attribute")->where(['key' => "registration"])->value("value");
        $registration_status = explode("|", $registration_status);
        foreach ($registration_status as $k => $v) {
            $registration_status[$k + 1] = $v;
        }
        unset($registration_status[0]);
        if (!empty($_GET['keyword'])) {
            $keyword = $_GET['keyword'];
            $where['b.name'] = ['like', "%{$keyword}%"];
        }
        //获取栏目ID
        $classify_id = Db::name("admin_content")->where(['id'=>$id])->value("classify_id");
        $this->assign("classify_id",$classify_id);
        //获取景区id
        $colony_id = Db::name("admin_content")->alias("a")->join("community_article_classify b","a.classify_id = b.id")->where(['a.id'=>$id])->value("b.colony_id");
        //查询已经报名的人员信息
        $where['b.colony_id'] = $colony_id;
        $where['a.content_id'] = $id;


        if(!empty($_GET['type'])){
            try {
                $page = empty($_GET['page']) ? 1 : $_GET['page'];
                $limit = empty($_GET['limit']) ? 10 : $_GET['limit'];
                $start = ($page - 1) * $limit;
                $list = Db::name("community_content_user")->alias("a")
                    ->join("system_registration b", "a.fans_id = b.id")
                    ->where($where)->field("a.*,b.name,b.sex,b.mobile")->limit($start,$limit)->select();

                $count = Db::name("community_content_user")->alias("a")
                    ->join("system_registration b", "a.fans_id = b.id")
                    ->where($where)->count("*");
                foreach ($list as $k => $v) {
                    $list[$k]['sex'] = $sex[$v['sex']];
                    $list[$k]['status'] = $registration_status[$v['status']];
                    $list[$k]['addtime'] = date("Y-m-d H:i:s",$v['addtime']);
        //                    $data[$k]['idCard'] = substr_replace($v['idCard'], str_repeat('*', 6), 6, 8);
        //                    $data[$k]['mobile'] = substr($v['mobile'], 0, 3) . '****' . substr($v['mobile'], 7, strlen($v['mobile']));;
                }
                $black['code'] = 0;
                $black['data'] = $list;
                $black['count'] = $count;
            }catch (Exception $e){
                $black['code'] = 202;
                $black['msg'] = $e->getMessage();
             }
            return $black;
        }


        $assign = [
            'keyword' => $keyword,
            'id' => $id
        ];
        $this->assign($assign);
        return $this->fetch('registration');
    }

    //查看报名名单详细信息
    public function registrationDetail(){
        $id = empty($_POST['id'])?0:$_POST['id'];
        $content = Db::name("community_content_user")->where(['id'=>$id])->find();

        //获取相关信息
        $type = Db::name("admin_content")->alias("a")->join("community_article_classify b","a.classify_id = b.id")->where(['a.id'=>$content['content_id']])->value("b.type");
        //性别
        $sex = Db::name("config_attribute")->where(['key' => "sex"])->value("value");
        $sex = explode("|", $sex);
        foreach ($sex as $k => $v) {
            $sex[$k + 1] = $v;
        }
        unset($sex[0]);
        //身体状况
        $condition = Db::name("config_attribute")->where(['key' => "condition"])->value("value");
        $condition = explode("|", $condition);
        foreach ($condition as $k => $v) {
            $condition[$k + 1] = $v;
        }
        unset($condition[0]);
        //学历
        $schooling = Db::name("config_attribute")->where(['key' => "schooling"])->value("value");
        $schooling = explode("|", $schooling);
        foreach ($schooling as $k => $v) {
            $schooling[$k + 1] = $v;
        }
        unset($schooling[0]);
        //义工类型
        $volunteers = Db::name("config_attribute")->where(['key' => "volunteers"])->value("value");
        $volunteers = explode("|", $volunteers);
        foreach ($volunteers as $k => $v) {
            $volunteers[$k + 1] = $v;
        }
        unset($volunteers[0]);
        //报名状态
        $registration_status = Db::name("config_attribute")->where(['key' => "registration"])->value("value");
        $registration_status = explode("|", $registration_status);
        foreach ($registration_status as $k => $v) {
            $registration_status[$k + 1] = $v;
        }
        unset($registration_status[0]);
        $this->assign("registration_status", $registration_status);
        //获取志愿者信息
        $registration = Db::name("system_registration")->where(['id'=>$content['fans_id']])->find();
        //获取小区信息
        $housing = Db::name("system_community_housing")->alias("a")
            ->join("system_community_element b","a.element_id = b.id")
            ->join("system_community_building c","a.building_id = c.id")
            ->join("system_community d","a.community_id = d.id")
            ->where(['a.id'=>$registration['housing_id']])
            ->field("a.roomNumber,b.elementName,c.buildingName,d.propertyName")->find();

        if(!empty($content['images'])){
            $content['images'] = explode(",",$content['images']);
        }

        $registration['housing'] = $housing['propertyName'].$housing['buildingName'].$housing['elementName'].$housing['roomNumber'];
        $registration['sex'] = $sex[$registration['sex']];
        $registration['condition'] = $condition[$registration['condition']];
        $registration['schooling'] = $schooling[$registration['schooling']];
        $registration['volunteers'] = $volunteers[$registration['volunteers']];
        $this->assign("registration", $registration);
        $this->assign("content", $content);
        $this->assign("type", $type);
        return view('registrationDetail');
    }

    //更新报名状态
    public function saveRegistration(){
        Db::startTrans();
        try{
            $id = $_POST['id'];
            $status = $_POST['status'];
            $common = new Common();
            $navList = Db::name("community_content_user")->where(['id'=>['in',$id]])->select();

            $integral = 0;
            foreach ($navList as $k=>$v){
                if($v['status']!=1){
                    throw new Exception("请勿重复操作");
                }
                if($status==2){
                    $integral = Db::name("admin_content")->where(['id'=>$v['content_id']])->value("integral");
                    $before_number = Db::name("system_registration")->where(['id'=>$v['fans_id']])->value("integral");
                    Db::name("system_registration")->where(['id'=>$v['fans_id']])->setInc("integral",$integral);
                    //记录日志
                    $data['reg_id'] = $v['fans_id'];
                    $data['remark'] = "用户参与活动获取积分";
                    $data['type'] = "integral";
                    $data['type_status'] = 1;
                    $data['number'] = $integral;
                    $data['before_number'] = $before_number;
                    $data['after_number'] = $before_number+$integral;
                    $data['addtime'] = time();
                    $data['operator_id'] = Session::get("id");
                    $data['auto'] =  Session::get("auto");
                    Db::name("system_registration_log")->insert($data);
                }
            }

            $navList = array_column($navList,"id");
            $navList = implode($navList,",");
            //报名状态
            $registration_status = Db::name("config_attribute")->where(['key' => "registration"])->value("value");
            $registration_status = explode("|", $registration_status);
            foreach ($registration_status as $k => $v) {
                $registration_status[$k + 1] = $v;
            }
            unset($registration_status[0]);
            $log['remark'] = "账号【" . Session::get("user_name") . "】更新志愿者报名名单id【".$navList."】,更改状态【".$registration_status[$status]."】";
            $log['controller'] = "Content";
            $log['methods'] = "saveRegistration";
            $common->addLog($log);
            $update['status'] = $status;
            $update['updatetime'] = time();
            $update['update_id'] = Session::get("id");
            Db::name("community_content_user")->where(['id'=>['in',$id]])->update($update);
            Db::commit();
            $black['code'] = 0;
            $black['msg'] = "操作成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }


    //后台指定报名
    public function addregistration(){
        $id = empty($_GET['id'])?0:$_GET['id'];
        $this->assign("id", $id);
        return $this->fetch('addregistration');
    }

    //删除名单
    public function delRegistration(){
        Db::startTrans();
        try{
            $id = $_POST['id'];
            $common = new Common();
            $navList = Db::name("community_content_user")->where(['id'=>['in',$id]])->field("fans_id")->select();
            $navList = array_column($navList,"fans_id");
            $navList = implode($navList,",");
            $log['remark'] = "账号【" . Session::get("user_name") . "】删除志愿者名单id【".$navList."】";
            $log['controller'] = "Content";
            $log['methods'] = "delRegistration";
            $common->addLog($log);
            Db::name("community_content_user")->where(['id'=>['in',$id]])->delete();
            Db::commit();
            $black['code'] = 0;
            $black['msg'] = "操作成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    //报名操作
    public function doRegistration(){
        Db::startTrans();
        try{
            $common = new Common();
            //获取志愿者信息
            $name = Db::name("system_registration")->where(['id'=>$_POST['fans_id']])->value("name");
            $data = $_POST;
            $detail = Db::name("community_content_user")->where($data)->value("id");
            if (!empty($detail)){
                throw new Exception("你以报名该活动");
            }
            //获取活动区间
            $content = Db::name("admin_content")->where(['id'=>$data['content_id']])->find();

            //获取对应栏目
            $type = Db::name("community_article_classify")->where(['id'=>$content['classify_id']])->value("type");
            /*if($content['start_time']>time()){
                throw new Exception("活动未开始");
            }
            if($content['end_time']<time()){
                throw new Exception("活动已结束");
            }*/
            if(empty($data['id'])){
                $data['addtime'] = time();
                $data['add_id'] = Session::get("id");
                Db::name("community_content_user")->insertGetId($data);
                if($type==9){
                    $log['remark'] = "用户【".$name."】加入队伍,队伍id【".$_POST['content_id']."】";
                }else{
                    $log['remark'] = "用户【".$name."】报名成功,活动id【".$_POST['content_id']."】";
                }

            }
            $log['controller'] = "Volunteers";
            $log['methods'] = "doData";
            $common->addLog($log);
            Db::commit();
            $black['code'] = 0;
            $black['msg'] = "报名成功";
        }catch (Exception $e){
            Db::rollback();
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }



    //获取导航下的栏目
    public function findClassify()
    {
        try {
            $nav_id = $_POST['nav_id'];
            $classify = Db::name("community_article_classify")->where(['nav_id' => $nav_id, 'pid' => 0,'type'=>['not in',[2,8]]])->field("id,name,type")->select();
            //获取下级栏目
            foreach ($classify as $k => $v) {
                $classify[$k]['child'] = Db::name("community_article_classify")->where(['pid' => $v['id'],'type'=>['not in',[2,8]]])->field("id,name,type")->select();
            }
            $black['code'] = 0;
            $black['msg'] = "添加成功";
            $black['data'] = $classify;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

    //获取栏目模型
    public function findType()
    {
        try {
            $id = $_POST['id'];
            $type = Db::name("community_article_classify")->where(['id' => $id])->value("type");
            $black['code'] = 0;
            $black['data'] = $type;
        } catch (Exception $e) {
            $black['code'] = 202;
            $black['msg'] = $e->getMessage();
        }
        return $black;
    }

}